// pages/mixuebingcheng/mixuebingcheng.js
Page({
  data: {
     List:[],
      longitude: '',
      latitude: '',
      name: '四川省眉山市',
      address: '锦江大道一号',
      markers:[],
      current_marker:'',
      now_state:true,
      num:0,
      height:0,
      
  },
onLoad(){
    var that = this
    wx.cloud.database().collection('true_shop').get()
    .then(res=>{
      var a=res.data
      for(var i=0;i<a.length;i++){
        var iii=a[i]
        a[i].distance=parseInt(that.disterpoint(iii.address_latitude,iii.address_longitude))
      }
      this.setData({
        List:a,
        longitude:wx.getStorageSync('userlocation').longitude,
        latitude:wx.getStorageSync('userlocation').latitude
      })
      this.getSite()
    })
},
disterpoint(latitude,longitude){
    console.log(wx.getStorageSync('userlocation').longitude);
    var x1 = wx.getStorageSync('userlocation').latitude * Math.PI / 180.0; 
    var x2 = latitude* Math.PI / 180.0;
    var x3 = x1 - x2;//a
    var y1=wx.getStorageSync('userlocation').longitude* Math.PI / 180.0; 
    var y2=longitude*Math.PI/180.0
    var y3=y1-y2//b
    var s=2*Math.asin(Math.sqrt(Math.pow(Math.sin(x3/2),2)+Math.cos(x1)*Math.cos(x2)*Math.pow(Math.sin(y3/2),2)))
    s=parseInt(s* 6371393)
    return s
},
Popup(e){
  var that=this
  var id=e.currentTarget.dataset.id
  that.setData({
    now_state:this.data.now_state==true?false:true,
    height:this.data.height=="850rpx"?0:'850rpx',
    num:id,
    current_marker:[{"latitude":this.data.List[id].address_latitude,"longitude":this.data.List[id].address_longitude,'id':id}]
  })
},
tanchu(){
    this.setData({
        now_state:this.data.now_state==true?false:true,
        height:this.data.height=="850rpx"?0:'850rpx'
    })
},
clickdirection(e){
  console.log(e.currentTarget.dataset.id);
  var id=e.currentTarget.dataset.id
  wx.openLocation({
    latitude: this.data.List[id].address_latitude,
    longitude: this.data.List[id].address_longitude,
  })
},
call(e){
  var id=e.currentTarget.dataset.id
  wx.makePhoneCall({
    phoneNumber:this.data.List[id].shop_phone_number,
  })
},
getSite(){
  var markers=[]
  for(var i=0;i<this.data.List.length;i++){
    markers.push({"latitude":this.data.List[i].address_latitude,'longitude':this.data.List[i].address_longitude,'id':i});
  }
  this.setData({
    markers
  })
},
order(){
    wx.navigateTo({
      url: '/pages/cart/cart',
    })
}
})